package com.flyme.module.base.provider.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.JsonUtils;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysSliderRemoteService;
import com.flyme.module.base.client.entity.SysConfig;
import com.flyme.module.base.client.entity.SysSlider;
import com.flyme.module.base.provider.service.SysConfigService;
import com.flyme.module.base.provider.service.SysSliderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 轮播图
 *
 * @author zyf
 */
@Api(tags = "轮播图管理")
@RestController
public class SysSliderController extends BaseController<SysSliderService, SysSlider> implements SysSliderRemoteService {

    @Autowired
    private SysConfigService configService;

    /**
     * 课程列表
     *
     * @return
     */
    @ApiOperation(value = "获取课程分页列表", notes = "获取课程分页列表")
    @Override
    public ResultBody list(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo, SysSlider slider) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("s.id,sliderTitle,sliderImg,jumpType,sendStatus,sliderType,s.createTime");
        cq.like("sliderTitle",slider.getSliderTitle());
        cq.eq("sliderType", slider.getSliderType());
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    @ApiOperation(value = "添加课程", notes = "添加课程")
    @Override
    public ResultBody add(@ModelAttribute SysSlider slider) {
        //查询轮播图类型
        Integer num = bizService.countByType(slider.getSliderType());
        if(slider.getSliderType() !=1 && slider.getSliderType() !=2 && slider.getSliderType() !=3 && num>0){
            boolean result = bizService.update(new UpdateWrapper().set(true, "sliderTitle", slider.getSliderTitle()).
                    set(true, "sliderImg", slider.getSliderImg()).eq(true, "sliderType", slider.getSliderType()));
            return ResultBody.success(result);
        }
        boolean result = bizService.add(slider);
        return ResultBody.success(result);
    }

    @ApiOperation(value = "通过id查询课程")
    @Override
    public ResultBody get(@PathVariable String id) {
        SysSlider slider = bizService.getById(id);
        return ResultBody.success().setData(slider);
    }

    @ApiOperation(value = "编辑课程", notes = "编辑课程")
    @Override
    public ResultBody edit(SysSlider slider) {
        boolean result= bizService.edit(slider);
        return ResultBody.success(result);
    }

    @ApiOperation(value = "删除课程", notes = "删除课程")
    @Override
    public ResultBody delByIds(@RequestParam String[] ids) {
        return bizService.delByIds(ids);
    }

    /**
     * 更新课程状态
     *
     * @param id 课程ID
     *@param sendStatus 是否上架
     * @return
     */
    @ApiOperation(value = "更新课程状态", notes = "更新课程状态")
    @Override
    public ResultBody setSendStatus(@RequestParam Long id, @RequestParam Integer sendStatus) {
        boolean result = bizService.update(new UpdateWrapper<SysSlider>().set("sendStatus",sendStatus).eq("id",id));
        return ResultBody.ok("设置成功").setData(result);
    }

    /**
     * 获取小程序海报
     */
    @ApiOperation(value = "更新课程状态", notes = "更新课程状态")
    @Override
    public ResultBody getPoster(){
        List<EntityMap> list = new ArrayList<>();
        EntityMap map = new EntityMap();
        SysConfig xcxHeard = configService.getByName("xcxHeard");
        SysConfig xcxRight = configService.getByName("xcxRight");
        SysConfig xcxLeft = configService.getByName("xcxLeft");
        map.put("xcxHeard", xcxHeard);
        map.put("xcxRight", xcxRight);
        map.put("xcxLeft", xcxLeft);
        list.add(map);
        return ResultBody.success(list);
    }

    @ApiOperation(value = "编辑小程序海报", notes = "编辑小程序海报")
    @Override
    public ResultBody editPoster(@RequestParam String subForm) {
        List<SysConfig> list = JsonUtils.toList(subForm, SysConfig.class);
        Boolean flag = configService.updateBatchById(list);
        return ResultBody.success(flag);
    }

}
